﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class myqueue : System.Web.UI.Page
{
    Customer cs;
    protected void Page_Load(object sender, EventArgs e)
    {
		//Standard code block to retrieve the customer object.
        if (Session["Customer"] != null)
        {
            cs = (Customer)Session["Customer"];
			
			//If the customer object is set, then load the customer's Queue for display on this page
            fillMyQueue(cs);
        }
        else
        {
			//If no customer object is set, force the user to login
            Response.Redirect("home.aspx");
        }

		//Get the latest featured movies from the Movies object.  Requires a database call.
		//The gvNewListings element is a gridview which displays movie details.  This is similar to searchmovies.aspx
        Movies mv = new Movies();
        gvNewListings.DataSource = mv.getNewMovies();
        gvNewListings.DataBind();
    }

    /// <summary>
    /// Load the My Queue details on the my queue page.
    /// </summary>
    /// <param name="cs">Customer object</param>
    protected void fillMyQueue(Customer cs)
    {
        Queue q = new Queue();
        DataSet ds = q.getCustomerQueueForDisplay(cs.Email); //the customer email is a unique identifier
        if (ds != null & ds.Tables.Count > 0)
        {
			//If the customer has some movies already in the queue, then display them.
            if (ds.Tables[0].Rows.Count > 0)
            {
                this.gvQueue.DataSource = ds;
            }
            else
            {
                this.gvQueue.EmptyDataText = "Please add some movies to your queue.";
            }
        }
        else
        {
            this.gvQueue.EmptyDataText = "Error loading the queue.  Please contact support.";
        }
        this.gvQueue.DataBind();
    }

	//This function is triggered when a user attempts to add a movie to their queue
    public void gvNewListings_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "New")
        {

            // Convert the row index stored in the CommandArgument
            // property to an Integer.
            int index = Convert.ToInt32(e.CommandArgument);

            // Get the last name of the selected author from the appropriate
            // cell in the GridView control.
            // GridViewRow selectedRow = gvNewListings.Rows[index];

            int index2 = Convert.ToInt32(e.CommandArgument);
            HiddenField myHiddenField2 = (HiddenField)gvNewListings.Rows[index2].Cells[0].FindControl("hdnMovieID2");

            Save(Convert.ToInt32(myHiddenField2.Value), "NewListings");
        }

    }

	//This function, used by gvNewListings_RowCommand(), will store changes to the user queue in the database.
    void Save(int movieID, string gridflag)
    {
        try
        {
            Movies mv = new Movies();
            int save = mv.saveQueue(cs.Email, movieID);
            if (save == -1)
            {
       
                    lblmsg2.Text = "This movie is already in your queue.";
            }
            else
            {
  
                  lblmsg2.Text = "Movie added to queue";
            }

        }
        catch (Exception Ex)
        {
            lblmsg2.Text = "Save Unsuccessful";
        }

    }
}
